'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE, TEXT, ENUM } = app.Sequelize;

  const User = app.model.define('user', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    username: {
      type: STRING(50),
      allowNull: false,
      unique: true,
      comment: '用户名',
    },
    email: {
      type: STRING(100),
      allowNull: false,
      unique: true,
      comment: '邮箱',
    },
    password: {
      type: STRING(255),
      allowNull: false,
      comment: '密码',
    },
    nickname: {
      type: STRING(50),
      allowNull: true,
      comment: '昵称',
    },
    avatar: {
      type: STRING(255),
      allowNull: true,
      comment: '头像URL',
    },
    gender: {
      type: ENUM('male', 'female', 'other'),
      allowNull: true,
      comment: '性别',
    },
    birthday: {
      type: DATE,
      allowNull: true,
      comment: '生日',
    },
    height: {
      type: INTEGER,
      allowNull: true,
      comment: '身高(cm)',
    },
    weight: {
      type: INTEGER,
      allowNull: true,
      comment: '体重(kg)',
    },
    bio: {
      type: TEXT,
      allowNull: true,
      comment: '个人简介',
    },
    status: {
      type: ENUM('active', 'inactive', 'banned'),
      defaultValue: 'active',
      comment: '用户状态',
    },
    last_login_at: {
      type: DATE,
      allowNull: true,
      comment: '最后登录时间',
    },
    // 时间戳字段由Sequelize自动管理
    // created_at, updated_at, deleted_at 会根据配置自动添加
  }, {
    tableName: 'users',
    comment: '用户表',
    indexes: [
      {
        fields: ['username'],
      },
      {
        fields: ['email'],
      },
    ],
  });

  User.associate = function() {
    // 用户拥有多个衣服
    app.model.User.hasMany(app.model.Clothing, {
      foreignKey: 'user_id',
      as: 'clothes',
    });

    // 用户拥有多个搭配
    app.model.User.hasMany(app.model.Outfit, {
      foreignKey: 'user_id',
      as: 'outfits',
    });

    // 用户拥有多个偏好设置
    app.model.User.hasMany(app.model.UserPreference, {
      foreignKey: 'user_id',
      as: 'preferences',
    });

    // 用户拥有多个穿搭记录
    app.model.User.hasMany(app.model.WearRecord, {
      foreignKey: 'user_id',
      as: 'wearRecords',
    });
  };

  return User;
};
